package me.leoray.leilog;

/**
 * Created by leoraylei on 16/8/28.
 */
public class BoxLogDecoration extends DetailLogDecoration {
    /**
     * 绘制日志边框需要的字符串
     */
    private static final char TOP_LEFT_CORNER = '╔';
    private static final char BOTTOM_LEFT_CORNER = '╚';
    private static final char MIDDLE_CORNER = '╟';
    private static final String VERTICAL_DOUBLE_LINE = "║ ";
    private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════";
    private static final String SINGLE_DIVIDER = "────────────────────────────────────────────";
    private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER + "\n";
    private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER + "\n";
    private static final String MIDDLE_BORDER = MIDDLE_CORNER + SINGLE_DIVIDER + SINGLE_DIVIDER + "\n";

    @Override
    public String[] process(int level, String tag, String msg) {
        StringBuilder builder = new StringBuilder();
        if (showThreadInfo) { //显示线程信息
            builder.append(TOP_BORDER);
            builder.append(VERTICAL_DOUBLE_LINE).append(getThreadName()).append('\n');
        }
        if (showThreadInfo) {
            builder.append(MIDDLE_BORDER);
        } else {
            builder.append(TOP_BORDER);
        }
        if (methodCount > 0) {
            final String methodStack = getMethodStack();
            String[] stackLines = methodStack.split("\n");
            for (int i = 0; i < stackLines.length; i++) {
                builder.append(VERTICAL_DOUBLE_LINE).append(stackLines[i]).append('\n');
            }
        }
        if (showThreadInfo || methodCount > 0) {
            builder.append(MIDDLE_BORDER);
        } else {
            builder.append(TOP_BORDER);
        }
        String[] msgLines = msg.split("\n");
        for (int i = 0; i < msgLines.length; i++) {
            builder.append(VERTICAL_DOUBLE_LINE).append(msgLines[i]).append('\n');
        }
        builder.append(BOTTOM_BORDER);
        return new String[]{builder.toString()};
    }

}
